package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.function.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2023/6/20 15:54
 */
public class WindowProcessDemo2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        env
            .socketTextStream("hadoop162", 8888)
            .map(new WaterSensorMapFunction())
            .keyBy(WaterSensor::getId)
            .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
            .reduce(
                new ReduceFunction<WaterSensor>() {
                    @Override
                    public WaterSensor reduce(WaterSensor value1,
                                              WaterSensor value2) throws Exception {
                        System.out.println("xxxx");
                        value1.setVc(value1.getVc() + value2.getVc());
                        return value1;
                    }
                },
                /*new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    
                    @Override
                    public void process(String key,
                                        Context ctx,
                                        // 存储前面聚合的最终结果: 有且仅有一个
                                        Iterable<WaterSensor> elements,
                                        Collector<String> out) throws Exception {
                        System.out.println("WindowProcessDemo2.process");
                        WaterSensor result = elements.iterator().next();
    
                        out.collect(ctx.window().getStart() + "     " + result);
                    }
                }*/
                new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void apply(String s,
                                      TimeWindow window,
                                      Iterable<WaterSensor> it,
                                      Collector<String> out) throws Exception {
        
                    }
                }
            )
            
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




/*
窗口处理函数:
    简单的
        sum  增量处理
        max min  增量处理
        maxBy minBy  增量处理
    
    复杂的
        reduce  增量处理
        
        aggregate 增量处理
        
        
        process 全量
 */